package testJdk.commonUse.number.testBigDecimal;

import cn.hutool.core.util.NumberUtil;
import org.junit.Test;

import java.math.BigDecimal;
import java.text.NumberFormat;

/**
 * @version 1.0.0
 * Created by cyd on 2023-07-05 22:51:55
 * @ClassName : testJdk.commonUse.number.testBigDecimal.BigDecimalCount
 * @Description : 类描述
 */
public class BigDecimalCount {

    /**
     * BigDecimal的加减乘除
     */
    @Test
    public void test() {
        BigDecimal num1 = new BigDecimal("0.005");
        BigDecimal num2 = new BigDecimal("1100000");
        BigDecimal num3 = new BigDecimal("-1000000");

        System.out.println(num2.add(num3));
        System.out.println(num2.subtract(num3));
        System.out.println(num1.multiply(num3));
        System.out.println(num2.divide(num3,2,BigDecimal.ROUND_HALF_UP));
        // 去除小数点后无用的0
        System.out.println(num2.divide(num3,2,BigDecimal.ROUND_HALF_UP).stripTrailingZeros().toPlainString());
    }

    /**
     * BigDecimal百分数
     */
    @Test
    public void test02() {
        BigDecimal num2 = new BigDecimal("100000");
        BigDecimal num3 = new BigDecimal("3000000");
        BigDecimal percent = num2.divide(num3, 4, BigDecimal.ROUND_HALF_UP);
        System.out.println(percent.doubleValue());
        // 具体保留几位，受入参和出参的双重制约
        String result = NumberUtil.decimalFormat("#.##%", percent.doubleValue());
        System.out.println(result);
    }
}
